package com.focus.xyz.union.pages.backend.tables;

import com.focus.xyz.core.common.keyword.Action;
import com.focus.xyz.core.pages.BaseTable;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

/**
 * Created by chenmeng on 2016/4/15.
 */
public class MemTable extends BaseTable {
    private Action action;
    @FindBy(xpath = "//*[@id=\"list00_table\"]")
    private WebElement memTable;

    @FindBy(xpath = "//*[@id=\"searchValue\"]")
    private WebElement userName;
    @FindBy(xpath = "//input[@name='searchBtn']")
    private WebElement searchBtn;
    @FindBy(xpath = "//*[@id=\"dev_auditRemark\"]")
    private WebElement desc;
    @FindBy(xpath = "//*[@id=\"auditLoginStatus_but\"]")
    private WebElement submitBtn;
    private static final String LINK_XPATH = "td[14]/a";
    public MemTable(WebDriver driver, Action action) {
        super(action);
        this.action = action;
        PageFactory.initElements(driver, this);
        super.table = memTable;
    }

    @Override
    protected By getLinkLocator() throws Exception {
        return By.xpath(LINK_XPATH);
    }

    @Override
    protected By getLocator(int index) throws Exception {
        return By.xpath("td[" + index + "]/div");
    }

    /*public void openLink(String key, String linkName) throws Exception {
        selectRowAndClickLink(memTable, key, linkName);
    }*/

    public void inputUserName(String username) throws Exception{
        action.inputText(userName,username);
    }

    public void inputDesc(String desc) throws Exception{
        action.inputText(this.desc,desc);
    }

    public void clickSubmitBtn() throws Exception{
        action.click(submitBtn);
    }

    public void clickSearchBtn() throws Exception{
        action.click(searchBtn);
    }

    public void search(String username) throws Exception{
        inputUserName(username);
        clickSearchBtn();
    }

    public void submit(String desc) throws Exception{
        inputDesc(desc);
        clickSubmitBtn();
        if(action.isAlertPresent()){
            action.acceptAlert();
        }
    }
}
